package com.goods.business.service.imp;

import com.goods.business.mapper.BizSupplierMapper;
import com.goods.business.service.BizSupplierService;
import com.goods.common.model.business.BizSupplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.google.common.base.Suppliers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BizSupplierServiceImpl implements BizSupplierService {
    @Autowired
    private BizSupplierMapper bizSupplierMapper;
    @Override
    public List<SupplierVO> selectPage(int pageNum, int pageSize, String name,String address,String contact) {
       List<BizSupplier> bizSupplierList = new ArrayList<>();
        BizSupplier bizSupplier = new BizSupplier();
        if (!StringUtils.isEmpty(name)){
            bizSupplier.setName(name);
        }
        if (!StringUtils.isEmpty(address)){
            bizSupplier.setAddress(address);
        }
        if (!StringUtils.isEmpty(contact)){
            bizSupplier.setContact(contact);
        }
        bizSupplierList = bizSupplierMapper.select(bizSupplier);
        List<SupplierVO> supplierVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(bizSupplierList)){
            for (BizSupplier bizSupplierOne : bizSupplierList) {
                SupplierVO supplierVO = new SupplierVO();
                BeanUtils.copyProperties(bizSupplierOne,supplierVO);
                supplierVOList.add(supplierVO);
            }
            supplierVOList=ListPageUtils.page(supplierVOList,pageSize,pageNum);
        }
        return supplierVOList;
    }

    @Override
    public void add(BizSupplier bizSupplier) {
        bizSupplier.setCreateTime(new Date());
        bizSupplier.setModifiedTime(new Date());
        bizSupplierMapper.insert(bizSupplier);
    }

    @Override
    public BizSupplier findById(long id) {
        BizSupplier bizSupplier = new BizSupplier();
        bizSupplier.setId(id);
        bizSupplier=bizSupplierMapper.selectOne(bizSupplier);
        return bizSupplier;
    }

    @Override
    public void update(BizSupplier bizSupplier) {
        bizSupplier.setModifiedTime(new Date());
        bizSupplierMapper.updateByPrimaryKeySelective(bizSupplier);
    }

    @Override
    public void deleteById(long id) {
        BizSupplier bizSupplier = new BizSupplier();
        bizSupplier.setId(id);
        bizSupplierMapper.delete(bizSupplier);
    }
}
